package com.pointwest.pls.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pointwest.pls.util.ConfigManager;

public class BaseDAO {
	
	
	public int insertValue() {
		 return 0;
	}
	
	
	public int deleteValue() {
		return 0;
	}

	/**retrieve data from database
	 * @param sqlStatement
	 * @return ResultSet selected from database
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws Exception
	 */
	public ArrayList<Map<String, String>> retrieveListValue(String sqlStatement, ArrayList<String> params, ArrayList<String> columns) throws ClassNotFoundException, SQLException, IndexOutOfBoundsException, Exception {
		
		Connection currentCon = ConfigManager.getInstance().getConnection();
		PreparedStatement prepStatement = null ;
		ResultSet result = null;
		ArrayList<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
		
		try {
			prepStatement = currentCon.prepareStatement(sqlStatement);
			for(int i = 0 ; i < params.size() ; i++){
				prepStatement.setString(i+1, params.get(i));
			}

			result = prepStatement.executeQuery();

			 while(result.next()){

					Map<String,String> resultMap = new HashMap<String, String>();
					for (int i = 0; i < columns.size(); i++) {
							
						resultMap.put(columns.get(i), result.getString(columns.get(i)));
							
					}
		
					resultList.add(resultMap);				
				}

			
	
			
			
		} catch (SQLException sqlExc) {
			sqlExc.printStackTrace();
		}
		
		finally {
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}
				
				if (currentCon != null) {
					currentCon.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		return resultList;
	}
	
	/**retrieve data from database
	 * @param sqlStatement
	 * @return ResultSet selected from database
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws Exception
	 */
	public HashMap<String, String> retrieveValue(String sqlStatement, ArrayList<String> params, ArrayList<String> columns) throws SQLException, Exception {
		

		Connection currentCon = ConfigManager.getInstance().getConnection();
		PreparedStatement prepStatement = null ;
		ResultSet result = null;
		HashMap<String, String> resultList = new HashMap<String, String>();
		
		try {
			prepStatement = currentCon.prepareStatement(sqlStatement);
			for(int i = 0 ; i < params.size() ; i++){
				prepStatement.setString(i+1, params.get(i));
			}

			result = prepStatement.executeQuery();

			while(result.next()){
					for (int i = 0; i < columns.size(); i++) {			
						resultList.put(columns.get(i), result.getString(columns.get(i)));							
					}
		
				} 
	
			
	
			
			
		} catch (SQLException sqlExc) {
			sqlExc.printStackTrace();
		}
		
		finally {
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}
				
				if (currentCon != null) {
					currentCon.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		return resultList;
	}
	
	/**update data from database
	 * @param sqlStatement
	 * @return int numbers of rows changed
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws Exception
	 */
	public int updateValue(String sqlStatement, ArrayList<String> params) throws SQLException, Exception{
		
		int rowsChanged = 0;
		Connection currentCon = ConfigManager.getInstance().getConnection();
		PreparedStatement prepStatement = null ;
		try {
			prepStatement = currentCon.prepareStatement(sqlStatement);
			for(int i = 0 ; i < params.size() ; i++){
				prepStatement.setString(i+1, params.get(i));
			}
			rowsChanged = prepStatement.executeUpdate();
		} catch (SQLException sqlExc) {
			sqlExc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				if (prepStatement != null) {
					prepStatement.close();
				}
				
				if (currentCon != null) {
					currentCon.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		return rowsChanged;
	}

}
